//index.js
import {request} from '../../request/index.js'
import regeneratorRuntime from '../../lib/runtime/runtime';
const md5 = require ('../../utils/md5.js')

Page({
  data: {
    // 用户登录信息获取
    username: '',
    password: '',
    code: '',
    yanzheng: 'https://www.heilama.com:8383/user/kaptcha',
    Usertoken: '',
    hid1:false,
    hid2: true,
  },
  // 手机密码账号登录
  async LoginIn(){
    wx.showLoading({
      title: '拼命加载中',
    })
    if (this.data.username && this.data.password && this.data.code) {
      var hashpass = this.data.password
      hashpass = md5.hex_md5(hashpass).toUpperCase()
      console.log(hashpass)
      var that = this
      const res = await request({
        url: "/user/login",data:{
          'username': that.data.username,
          'password': hashpass,
          'code':that.data.code
        },
        method: "post"
      })
      console.log(res)
      if (res.data.status !== 200){
        var title = res.data.msg ? res.data.msg :null
        if (title){
           wx.showToast({
            title: title,
            icon: 'none',
            duration: 2000
          })
        }
       
        that.setData({
          // username: '',
          // password: '',
          code: ''
        }),
        that.changeImg()
      } else {
        wx.setStorageSync('username', res.data.data.username)
        wx.setStorageSync('token', res.data.data.token)
        wx.setStorageSync('name', res.data.data.empName)
        wx.setStorageSync('empId', res.data.data.empId)
        wx.setStorageSync('role', res.data.data.userRole)
        that.getInfos()
        wx.redirectTo({
          url: '/pages/my/my'
         });
      }
    }
    else {
      if(this.data.isShowLoading){
        wx.hideLoading()
      }
      wx.showToast({
        title: '用户名、密码、验证码不能为空',
        icon: 'none',
        duration: 2000
      })
    }
    
  },
  // username获取
  usernameInput:function(e){
    this.setData({
      username: e.detail.value
    })
    // console.log(this.data.username)
  },
  // password获取
  passwordInput:function(e){
    this.setData({
      password: e.detail.value
    })
    // console.log(this.data.password)
  },
  // code获取
  codeInput:function(e){
    this.setData({
      code:e.detail.value
    })
  },

// 刷新获取新验证码
  changeImg:function(e){
    var num = Math.ceil(Math.random()*10);
    this.setData({
      yanzheng: "https://www.heilama.com:8383/user/kaptcha?" + num
    })
  },
  
// 获取用户微信名称
 bindGetUserInfo:function(e){ 
   console.log(e.detail.errMsg)   
   if(e.detail.errMsg != "getUserInfo:fail auth deny"){
    this.setData({
      hid1: true,
      hid2: false
    })
     wx.setStorageSync('name', e.detail.userInfo.nickName)
   console.log(wx.getStorageSync('name'))
   }
   else{
    if(this.data.isShowLoading){
      wx.hideLoading()
    }
      wx.showToast({
        title: '若不授权，您将无法使用该小程序功能，请您重新考虑！',
        icon: 'none',
        duration: 4000
      })
      wx.navigateBack({
        delta: 0,
      })
   }
  },
  // 获取用户手机号码
  getPhoneNumber: function(e) {
    let that = this;
    const app = getApp();
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      wx.request({
        url: 'https://www.heilama.com:8383/user/weixinLogin',
        data: {
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          session_key: app.globalData.session_key
        },
        method: "post",
        success: function (res){   
          console.log(res)
          var usertoken = res.data.data.token
          var username = res.data.data.username
          console.log(username)
          wx.setStorageSync('empId', res.data.data.empId)
          wx.setStorageSync('username', username)
          wx.setStorageSync('token', usertoken)
          wx.setStorageSync('role', res.data.data.userRole)
          // wx.setStorageSync('deptId', res.data.data.deptId)
          console.log(wx.getStorageSync('username')) 
          that.getInfos();
          wx.redirectTo({
          url: '/pages/my/my'
          });
          }
        })  
        // if (wx.getStorageSync('empId')){
         
        // }
      }
    },
  //事件处理函数
  bindViewTap: function() {
    
  },
  onLoad: function () {
    // wx.setStorageSync('checked', true)
    // wx.setStorageSync('doing', false)
  },

async getInfos(){
  const res = await request({
    url: '/emp/showInfo/' + wx.getStorageSync('empId'),
    header:{
      'Authorization':'Bearer '+wx.getStorageSync('token'),
    },
    method: "get",
  })
  console.log(res)
  if (res.data.status !== 200){
    var title = res.data.msg
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
    // if (title == '您还未登录'){
    //   wx.reLaunch({
    //     url: '/pages/index/index'
    //   });
    // }
  }
  else {
    console.log(res.data)
    wx.setStorageSync('deptId', res.data.data.deptId)
  }
},

})

